package net.javawind.t_api.apps;

import java.awt.Desktop;
import java.net.URI;
import java.util.Scanner;

import net.javawind.t_api.beans.OAuth;
import net.javawind.t_api.commons.Statuses_API;
import net.javawind.t_api.commons.User_API;
import net.javawind.t_api.utils.OAuthClient;

/**
 * 测试类
 * 
 * @author <a href="http://blog.javawind.net">Xuefang Xu</a>
 * @Data 2010-01-21
 * @Version 1.0.0
 */

public class MainDemo {

	static final String appKey = "5ce95aae9954450b9b0b62943d93fef2";
	static final String appSecret = "488ebabd8db2391915cb3daceb1f7c50";
	static String callBackURL = "http://www.sdo.com/?s=qq";
	static String format = "xml";
	static boolean skipLogin = false;

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		if (skipLogin) {
			skipLogin();
			return;
		}
		// TODO Auto-generated method stub
		OAuth oauth = new OAuth(appKey, appSecret, callBackURL); // 初始化OAuth请求令牌
		OAuthClient auth = new OAuthClient();// OAuth 认证对象

		oauth = auth.requestToken(oauth);
		if (oauth.getStatus() == 1) {
			System.out.println("Request Token 授权失败！");
			return;
		} else {
			String url = "http://open.t.qq.com/cgi-bin/authorize?oauth_token="
					+ oauth.getOauth_token();
			URI uri = new URI(url);
			if (!Desktop.isDesktopSupported()) {
				System.err.println("浏览器启动出错，请手动访问：" + url + "，并获取授权码");
			} else {
				Desktop desktop = Desktop.getDesktop();
				if (desktop == null
						|| !desktop.isSupported(Desktop.Action.BROWSE)) {
					System.err.println("浏览器启动出错，请手动访问：" + url + "，并获取授权码");
				}
				try {
					desktop.browse(uri);
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(1);
				}
			}

			Scanner in = new Scanner(System.in);
			System.out.println("请输入你的授权码：");
			String oauth_verifier = in.nextLine().trim();

			oauth.setOauth_verifier(oauth_verifier);
			oauth = auth.accessToken(oauth);

			if (oauth.getStatus() == 2) {
				System.out.println("Access Token 授权失败！");
				return;
			} else {
				
				System.out.println("Oauth_token : "+oauth.getOauth_token());
				System.out.println("Oauth_token_secret : "+oauth.getOauth_token_secret());
				
				// 这里你就可以使用 oauth 对象自由访问API了

				// 比如,获取 主页时间线:
				Statuses_API sapi = new Statuses_API();
				String result = sapi.home_timeline(oauth, "json", "0", "", "20");
				System.out.println(result);
				
				User_API uapi = new User_API();
				System.out.println(uapi.info(oauth, "json"));

				// 发表微博信息
				//T_API tapi = new T_API();
				// result = tapi.add(oauth, "json", "#TEST#来自趣博墙应用的微博", "123.45.67.89");
				//String result = tapi.add_pic(oauth, "json","#趣文囧图寻开心，野火手机拿不停# 雪人也疯狂", "123.45.67.89","D:/1_090812162552_1.jpg");
				//System.out.println(result);
			}
		}
	}

	/**
	 * 第一次授权通过后，可以通过打印oauth对象的oauth_token和oauth_token_secret
	 * 填到这里测试使用，可以免除重复登陆和授权
	 * @throws Exception
	 */
	private static void skipLogin() throws Exception {
		OAuth oauth = new OAuth(appKey, appSecret, callBackURL); // 初始化OAuth请求令牌
		oauth.setOauth_token("0e4e726d9e934d9cbe77ac35aad3f1f6");
		oauth.setOauth_token_secret("1e48da1aebfa90922a93a3956c9379b8");

		// 比如,获取 主页时间线:
		Statuses_API sapi = new Statuses_API();
		String result = sapi.home_timeline(oauth, "json", "0", "", "20");
		System.out.println(result);
		
		//T_API tapi = new T_API();
		//result = tapi.add_pic(oauth, "json", "#趣文囧图寻开心，野火手机拿不停#", "123.45.67.89","D:/1804141_1.jpg");
		//System.out.println(result);
	}
}
